package com.opc.Config;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;  
import org.springframework.web.socket.WebSocketMessage;  

public class MyWebSocketHandler extends TextWebSocketHandler {

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 处理消息
        String msg = message.getPayload();  // 获取消息的有效载荷
        System.out.println("Received message: " + msg);
        session.sendMessage(new TextMessage("Message received: " + msg));  // 反馈消息
    }

    // 如果需要在打开和关闭连接时处理，可以重写以下方法  
    @Override  
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {  
        System.out.println("WebSocket connection established: " + session.getId());  
    }  

    @Override  
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("WebSocket connection closed: " + session.getId());  
    }  
}  